package com.yanqu.road.server.cmd.activity.tonbing;

import com.yanqu.road.entity.activity.tonbing.config.TonBingConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.TonBingProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.tonbing.TonBingMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_TON_BING_SYNC_CITY_TEAM, desc = "")
public class TonBingGetCityTeamCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        TonBingConfig config = TonBingMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if (!config.inEnterActivityTime()) {
            ret = GameErrorCode.E_TON_BING_NO_OPERATE_TIME;
            return;
        }

        TonBingProto.TonBingSyncCityTeamReqMsg reqMsg = TonBingProto.TonBingSyncCityTeamReqMsg.parseFrom(packet.getBytes());

        // 跨服执行操作
        TonBingProto.CrossTonBingSyncCityTeamReqMsg.Builder builder = TonBingProto.CrossTonBingSyncCityTeamReqMsg.newBuilder();
        builder.setActivityId(config.getActivityInfo().getActivityId());
        builder.setReq(reqMsg);
        player.sendPacket(CrossProtocol.C2_TON_BING_SYNC_CITY_TEAM, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }

        TonBingProto.TonBingSyncCityTeamRespMsg.Builder respMsg = TonBingProto.TonBingSyncCityTeamRespMsg.newBuilder();
        player.sendPacket(ClientProtocol.U_TON_BING_SYNC_CITY_TEAM, respMsg);
    }
}
